package com.parse.c0003.starter;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.bean.LoginDTO;
import com.parse.starter.R;
import com.parse.starter.R.id;
import com.parse.starter.R.layout;

public class LoginActivity extends Activity {

	private LoginDTO dto = new LoginDTO();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.login_layout);
		this.findViewById(R.id.login).setOnClickListener(new login());

	}

	private class login implements OnClickListener {

		@Override
		public void onClick(View v) {

			final String userId = ((EditText) findViewById(R.id.userId))
					.getText().toString();
			final String passowrd = ((EditText) findViewById(R.id.password))
					.getText().toString();

			ParseUser.logInInBackground(userId, passowrd, new LogInCallback() {
				public void done(ParseUser user, ParseException e) {
					if (user != null) {
						Toast.makeText(getApplicationContext(), "login",
								Toast.LENGTH_SHORT).show();

						dto.setEmail(user.getString("email"));
						dto.setPhone(user.getString("phone"));
						dto.setUserId(user.getString("username"));

						getIntent().putExtra("loginDTO", dto);
						setResult(Activity.RESULT_OK, getIntent());
						finish();

						// Hooray! The user is logged in.
					} else {

						Toast.makeText(getApplicationContext(), "WARM",
								Toast.LENGTH_SHORT).show();
						;
						// Signup failed. Look at the ParseException to
						// see what happened.
					}
				}
			});

		}
	}

	public LoginDTO getDto() {
		return dto;
	}

	public void setDto(LoginDTO dto) {
		this.dto = dto;
	}

}
